package lock;

import org.junit.Test;
import org.openjdk.jol.info.ClassLayout;
import org.openjdk.jol.vm.VM;
import sun.misc.Unsafe;

import java.lang.reflect.Field;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @author: feiwang_6
 * @create: 2020/4/30 17:47
 * @description:
 */
public class ReentrantLockTest {
    
    @Test
    public void reentrantLock(){
        ReentrantLock lock = new ReentrantLock();
        lock.lock();
        System.out.println("1111111111111");
        lock.unlock();

        ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();
        
    }
    
    
    @Test
    public void testUnsafe() throws Exception {
        
        System.out.println(VM.current().details());
        System.out.println(ClassLayout.parseInstance(new VO()).toPrintable());
        System.out.println("=================");
        
        Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
        theUnsafe.setAccessible(true);
        Unsafe unsafe = (Unsafe) theUnsafe.get(null);
        
        VO vo = new VO();
        vo.a = 10;
        vo.b = 20;
        long aoffset = unsafe.objectFieldOffset(VO.class.getDeclaredField("a"));
        long boffset = unsafe.objectFieldOffset(VO.class.getDeclaredField("b"));
        System.out.println("a字段的offset: " + aoffset);
        System.out.println("b字段的offset: " + boffset);
        System.out.println("根据offset获取属性值=================");
        System.out.println(unsafe.getInt(vo, aoffset));
        System.out.println(unsafe.getInt(vo, boffset));
    }
}
